/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.scs.ubbcluj.view;

import edu.scs.ubbcluj.backend.MongoDB;
import edu.scs.ubbcluj.model.Attribute;
import edu.scs.ubbcluj.model.Table;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 *
 * @author Torok
 */
public class DBTableView {

    @FXML
    private TableView tableView;

    @FXML
    public static TableView createTable(Table table) {
        TableView tableView = new TableView();
        List<String> columns = new ArrayList<>();
        for (Attribute attr: table.getPrimaryKeys()){
            columns.add(attr.getName());
        }
        for (Attribute attr: table.getAttributes()){
            columns.add(attr.getName());
        }
        for (Attribute attr: table.getForeignKeys()){
            columns.add(attr.getName());
        }
        TableColumn[] tableColumns = new TableColumn[columns.size()];
        for (int i = 0; i < columns.size(); i++) {
            final int j = i;
            TableColumn col = new TableColumn(columns.get(i));
            col.setMinWidth(100);
            col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });
            tableView.getColumns().addAll(col);
        }
        
        List<List<String>> values= MongoDB.getAll(table);
        for (List<String> list: values){
            ObservableList<String> row = FXCollections.observableArrayList();
            for (String value:list){
                row.addAll(value);
            }
            tableView.getItems().add(row);
        } 
        return tableView;
    
    }
    
    public static TableView createTable(List<Attribute> columns, List<List<String>> values, List<Attribute> notNeeded) {
        TableView tableView = new TableView();
        
        if(notNeeded!=null) {
            for(Attribute atr:notNeeded) {
                int ind = columns.indexOf(atr);
                if(ind!=-1) {
                    columns.remove(atr);
                    for (List<String> list: values){
                        list.remove(ind);
                    } 
                }
            }
        }
        
        for (int i = 0; i < columns.size(); i++) {
            final int j = i;
            TableColumn col = new TableColumn(columns.get(i).getName());
            col.setMinWidth(100);
            col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });
            tableView.getColumns().addAll(col);
        }
        
        for (List<String> list: values){
            ObservableList<String> row = FXCollections.observableArrayList();
            for (String value:list){
                row.addAll(value);
            }
            tableView.getItems().add(row);
        } 
        
        return tableView;
    
    }

}
